package tichu_project252.Round;

import java.util.Collections;
import java.util.LinkedList;
import tichu_project252.CardClasses.Card;
import tichu_project252.Table;

public class fullHouse extends TurnType {

    /**
     * Returns true if the combination given is full house.
     *
     * <p> <b> Accessor(Selector): </b> isfullHouse(LinkedList <Card> l, Table t) </p>
     *
     * <p><b> Precondition: </b> The player should select some cards.</p> </p>
     * <p> <b> Postcondition:</b> Returns true if the combination is a full
     * house, else it returns false. </p>
     *
     * @return if the combination is full house.
     */
    public static boolean isfullHouse(LinkedList<Card> l, Table t) {
        
        if (l.size() == 5) {  // full house has only 5 cards

            Collections.sort(l, new CardComparator());  //sort the cards
            
            LinkedList<Card> pair = new LinkedList<Card>(); //we store the pair here
            LinkedList<Card> triple = new LinkedList<Card>(); //we store the triple here
            LinkedList<Integer> temp = new LinkedList<Integer>(); //we store the int representations

            //add all intReprs of the full house into the templist
            for (int i = 0; i < l.size(); i++) {
                temp.add(l.get(i).getintRepr());
            }
            //sort it
            Collections.sort(temp);
            //remove duplicates
            temp = removeDuplicates(temp);
            //now we've got the following cases:
            //if the size of the stripped down integer list is 2, then 
            //we probably have a full house
            if (temp.size() == 2) {
                //depending on the center cards we distribute 
                //the triple and the pair of the fullHouse
                //in order to check them for errors later
                if (l.get(2).getintRepr() != l.get(3).getintRepr()) {
                    triple.add(l.get(0));
                    triple.add(l.get(1));
                    triple.add(l.get(2));
                    pair.add(l.get(3));
                    pair.add(l.get(4));
                } else {
                    pair.add(l.get(0));
                    pair.add(l.get(1));
                    triple.add(l.get(2));
                    triple.add(l.get(3));
                    triple.add(l.get(4));
                }
                //if the size of the integer list is 3, then we've probably have 
                //a phoenix card in there
            } else if (temp.size() == 3) {
                //if the first and the second cards differ, then the single card 
                //is the first one e.g. 2, 3, 3, 3, P
                //so, the phoenix must be paired with the lone 2
                if (l.get(0).getintRepr() != l.get(1).getintRepr()) {
                    triple.add(l.get(1));
                    triple.add(l.get(2));
                    triple.add(l.get(3));
                    pair.add(l.get(0));
                    //while we're at it, configure the phoenix to its pair's intRepr
                    l.get(4).setintRepr(l.get(0).getintRepr());
                    pair.add(l.get(4));
                //if the third and fourth cards differ, then the single card 
                //is the fourth one e.g. 2, 2, 2, 3, P
                //so, the phoenix must be paired with the lone fourth card
                } else if (l.get(2).getintRepr() != l.get(3).getintRepr())  {
                    triple.add(l.get(0));
                    triple.add(l.get(1));
                    triple.add(l.get(2));
                    pair.add(l.get(3));                    
                    //while we're at it, configure the phoenix to its pair's intRepr
                    l.get(4).setintRepr(l.get(3).getintRepr());
                    pair.add(l.get(4));
                //then we're in a 2, 2, 3, 3, P situation
                //so, add phoenix to the greater of the two pairs
                } else if (l.get(1).getintRepr() != l.get(2).getintRepr()) {
                    //while we're at it, configure the phoenix to its pair's intRepr
                    pair.add(l.get(0));   
                    pair.add(l.get(1));             
                    triple.add(l.get(2));
                    triple.add(l.get(3));                    
                    l.get(4).setintRepr(l.get(3).getintRepr());
                    triple.add(l.get(4));
                }
            } else {
                return false;
            }

            System.out.println("triple: " + triple + " pair: " + pair);

            if (t.getLastDropped().size() == 0) {
                return Pair.isPair(pair, t) && Triple.isTriple(triple, t);
            } else if (t.getLastDropped().size() == 5) {
                LinkedList<Card> previous = t.getLastDropped();

                System.out.println("previous: " + previous);

                if (previous.get(2).getintRepr() < triple.get(0).getintRepr()) {
                    return Pair.is(pair) && Triple.isTriple(triple, t);
                }
            } else {
                return false;
            }
        }
        return false;
    }

}
